/**
 * Created by wangkui on 2016/1/14.
 */
var request = require('request');
var config = require('./../config');
var bigpixelsrv = require('./bigpixelsrv');
//更新为redis数据缓存方式{2015-12-23 created by wangkui}
var CachemanRedis = require('cacheman-redis');
var cache = new CachemanRedis(config.cachestore);
var express = require('express');
var router = express.Router();
//留言用户名单
var roomInfo = {};
//被叫用户ID
var user_called_id;
//用户一对一留言
exports.userchat=function(socketIO){
    socketIO.on('connection', function (socket) {
        var connectedSockets={};
        // 获取请求建立socket连接的url
        // 如: http://localhost:3000/room/room_1, roomID为room_1或者为room_123_321
        // room_123表示主叫方呼叫被叫方(123)刚刚进入，room_123_321(123进入321给自己留言的界面).
        var url = socket.request.headers.referer;
        var splited = url.split('/');
        var roomID = splited[splited.length - 1];   // 获取被叫方ID
        var useridlist=roomID.replace('room_','');  // 获取被叫方+“_”+主叫方ID
        var splited_useridlist=useridlist.split('_');
        if(splited_useridlist.length===1){
            user_called_id=useridlist;
        }else{
            user_called_id=splited_useridlist[splited_useridlist.length-2];
        }
        var user = '';

        //userid为呼叫方用户ID
        //当前roomID为:room_5639aaaaa2a9dea02b1ac651 roomid+被叫方用户ID
        socket.on('join', function (userid,userName) {
            user = userName;
            if(userid!=user_called_id){
                // 生成一对一对话编号
                roomID=roomID+'_'+userid;
                if (!roomInfo[roomID]) {
                    roomInfo[roomID] = [];
                }
                roomInfo[roomID].push(user);
                socket.join(roomID);
                connectedSockets[roomID]=socket;
                // 通知人员加入
                connectedSockets[roomID].emit('sys', user + '加入了', roomInfo[roomID]);
                console.log(user + '加入了' + roomID);
            }
        });

        socket.on('leave', function () {
            socket.emit('disconnect');
        });

        socket.on('disconnect', function () {
            // 退出
            var index = roomInfo[roomID].indexOf(user);
            if (index !== -1) {
                roomInfo[roomID].splice(index, 1);
            }

            socket.leave(roomID);    // 退出谈话
            //socketIO.to(roomID).emit('sys', user + '退出了交谈', roomInfo[roomID]);
            connectedSockets[roomID].emit('sys', user + '退出了交谈', roomInfo[roomID]);
            console.log(user + '退出了' + roomID);
        });

        //用户正在输入
        socket.on('typing', function () {
            connectedSockets[roomID].broadcast.emit('typing', {
                username: user
            });
        });
        //用户停止输入
        socket.on('stop typing', function () {
            connectedSockets[roomID].broadcast.emit('stop typing', {
                username: socket.username
            });
        });
        // 接收用户消息,发送相应的房间
        socket.on('message', function (msg) {
            // 验证如果用户离开则不给发送
            if (roomInfo[roomID].indexOf(user) === -1) {
                return false;
            }else{
            // 消息保存到数据库中，redis/mongodb中

            }
            //socketIO.to(roomID).emit('msg', user, msg);
            connectedSockets[roomID].emit('msg', user, msg);
        });
    });
}